//setting up testing
//http://developer.android.com/guide/developing/testing/testing_eclipse.html
package activities;

import bookless.pakage.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BooklessActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button login;
		login = (Button) findViewById(R.id.login);
		login.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent("bookless.pakage.LOGIN"));
			}
		});

		Button newUser;
		newUser = (Button) findViewById(R.id.newUser);
		newUser.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent("activities.REGISTER"));
			}
		});

		Button search;
		search = (Button) findViewById(R.id.search);
		search.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent("bookless.pakage.SEARCH"));
			}
		});

		Button admin;
		admin = (Button) findViewById(R.id.adminbutton);
		admin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {


				adminLogin();

			}
		});

	}
	
	/*
	 * 
	 * @AdminLogin() creates a LinearLayout with two TextViews for Admin login user 
	 * name and password and a submit button. Checks if user name and password match 
	 * the string "admin" and if successful forwards the user to AdminActivity.
	 * 
	 * */
	
	public void adminLogin(){

	LinearLayout listLayout = new LinearLayout(getApplicationContext());
	setContentView(listLayout);
	listLayout.setOrientation(LinearLayout.VERTICAL);

	String text = "Admin name";
	TextView tv = new TextView(getApplicationContext());
	tv.setText(text);
	listLayout.addView(tv);

	final EditText name = new EditText(getApplicationContext());
	listLayout.addView(name);

	String text1 = "Admin password";
	TextView tv1 = new TextView(getApplicationContext());
	tv1.setText(text1);
	listLayout.addView(tv1);

	final EditText password = new EditText(getApplicationContext());
	listLayout.addView(password);

	Button login = new Button(getApplicationContext());
	login.setWidth(6);
	login.setText("Login");
	listLayout.addView(login);
	
	login.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			if (name.getText().toString().equals("admin")
					&& password.getText().toString().equals("admin")) {
				startActivity(new Intent("bookless.pakage.ADMIN"));
			}else{
        		Dialog d = new Dialog(BooklessActivity.this);
				d.setTitle("Error!");
				TextView tv = new TextView(BooklessActivity.this);
				tv.setText("login details do not match");
				d.setContentView(tv);
				d.show();

			}
			
		}
	});
	}
}